package com.cheyun.netsalev3.repository

import androidx.paging.PageKeyedDataSource
import com.cheyun.netsalev3.bean.*
import com.cheyun.netsalev3.utils.api.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class MissedTellistPageRepository : BasePageRepository<MissedTellistParam>() {
    override fun loadInitial(params: PageKeyedDataSource.LoadInitialParams<Int>, callback: (data: MutableList<MissedTellistParam>) -> Unit) {
        ApiClient.getInstance().getMissedTellist(1)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<MissedTelParam>>() {
                    override fun onSuccess(t: BaseResp<MissedTelParam>) {
                        t.data?.rows?.apply {
                            t.data?.rows?.forEach { c->if(c.is_archive.equals("已建档")) c.archive = 1 else c.archive = 0 ; if(c.phone.indexOf("*")!=-1) c.enableMsg = true else c.enableMsg = false}
                            total.postValue(t.data!!.total)
                            callback(this.toMutableList())
                        }
                    }

                    override fun onError(ex: ApiException) {
                        networkState.postValue(NetworkState.error(ex.getMessage()))
                    }

                })
    }

    override fun loadAfter(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, MissedTellistParam>) {
        ApiClient.getInstance().getMissedTellist(params.key)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<MissedTelParam>>() {
                    override fun onSuccess(t: BaseResp<MissedTelParam>) {
                        t.data?.rows?.apply {
                            t.data?.rows?.forEach { c->if(c.is_archive.equals("已建档")) c.archive = 1 else c.archive = 0 ; if(c.phone.indexOf("*")!=-1) c.enableMsg = true else c.enableMsg = false}
                            callback.onResult(this, params.key + 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }

                })

    }

    override fun loadBefore(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, MissedTellistParam>) {
        ApiClient.getInstance().getMissedTellist(params.key)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<MissedTelParam>>() {
                    override fun onSuccess(t: BaseResp<MissedTelParam>) {
                        t.data?.rows?.apply {
                            t.data?.rows?.forEach { c->if(c.is_archive.equals("已建档")) c.archive = 1 else c.archive = 0;  if(c.phone.indexOf("*")!=-1) c.enableMsg = true else c.enableMsg = false}
                            callback.onResult(this, params.key - 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }

                })
    }

    /**
     * 检查电话号码是否存在
     */
    fun IsPhone(phone: String, kFunction1: (item: IsPhoneParam) -> Unit) {
        ApiClient.getInstance().isPhone(phone)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<IsPhoneParam>>() {
                    override fun onSuccess(t: BaseResp<IsPhoneParam>) {
                        t.data?.apply {
                            kFunction1(this)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

}